using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Markup;
using Common;
using MainView;
using MainView.MainControls;
using SExperiment;
using SSettings;

namespace Startup
{
	public partial class HomeControl : UserControl, IComponentConnector, IStyleConnector
	{
		public MainWindow MainWindow { get; set; }

		private void OnQuickExperimentManageChanged_RoutedEvent(object sender, RoutedEventArgs e)
		{
			if (!(e is QuickExperimentListControl.QuickExperimentManageChangedEventArgs))
			{
				return;
			}
			QuickExperimentListControl.QuickExperimentManageChangedEventArgs quickExperimentManageChangedEventArgs = e as QuickExperimentListControl.QuickExperimentManageChangedEventArgs;
			QuickExperimentListControl.ManageChangedType type = quickExperimentManageChangedEventArgs.type;
			if (type == QuickExperimentListControl.ManageChangedType.Open)
			{
				string empty = string.Empty;
				if (quickExperimentManageChangedEventArgs.qd != null)
				{
					empty = quickExperimentManageChangedEventArgs.qd.QuickExperimentName;
					MainWindow.OpenExperimentFromHomeControl(empty, false, quickExperimentManageChangedEventArgs.qd.QuickDefaultPath);
				}
			}
		}

		public HomeControl()
		{
			InitializeComponent();
			AddHandler(QuickExperimentListControl.OnQuickExperimentManageChangedRoutedEvent, new RoutedEventHandler(OnQuickExperimentManageChanged_RoutedEvent), true);
			List<string> list = new List<string>();
			foreach (string item in ConfigSettings.GetInstance().History.ExperimentHistory)
			{
				ExperimentTemporary experimentTemporaryInfo = Experiment.GetExperimentTemporaryInfo(item);
				if (experimentTemporaryInfo == null)
				{
					list.Add(item);
				}
			}
			foreach (string item2 in list)
			{
				ConfigSettings.GetInstance().History.RemoveExperimentHistory(item2);
			}
			listBoxHistory.ItemsSource = ConfigSettings.GetInstance().History.ExperimentHistory;
		}

		private void btnExperimentGuid_Click(object sender, RoutedEventArgs e)
		{
			MainWindow.CreateExperimentFromHomeControl(EExperimentCreateMode.Guid);
		}

		private void btnExperimentQuick_Click(object sender, RoutedEventArgs e)
		{
			MainWindow.CreateExperimentFromHomeControl(EExperimentCreateMode.Quick);
		}

		private void btnExperimentWellPlate_Click(object sender, RoutedEventArgs e)
		{
			MainWindow.CreateExperimentFromHomeControl(EExperimentCreateMode.WellPlate);
		}

		private void btnExperimentTemplate_Click(object sender, RoutedEventArgs e)
		{
			MainWindow.CreateExperimentFromHomeControl(EExperimentCreateMode.Template);
		}

		private void btnRunLast_Click(object sender, RoutedEventArgs e)
		{
			MainWindow.CreateExperimentFromHomeControl(EExperimentCreateMode.Last);
		}

		private void btnExperimentOpen_Click(object sender, RoutedEventArgs e)
		{
			MainWindow.CreateExperimentFromHomeControl(EExperimentCreateMode.OpenFile);
		}

		private void listboxHistory_OpenItem_Click(object sender, RoutedEventArgs e)
		{
			MenuItem menuItem = sender as MenuItem;
			if (menuItem != null || menuItem.DataContext != null)
			{
				MainWindow.OpenExperimentFromHomeControl(menuItem.DataContext.ToString());
			}
		}

		private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
		{
			Grid grid = sender as Grid;
			if (grid != null && grid.DataContext != null)
			{
				MainWindow.OpenExperimentFromHomeControl(grid.DataContext.ToString());
			}
		}

		private void listboxHistory_OpenItemFolder_Click(object sender, RoutedEventArgs e)
		{
			MenuItem menuItem = sender as MenuItem;
			if (menuItem != null || menuItem.DataContext != null)
			{
				string path = menuItem.DataContext.ToString();
				Utility.RunCommand(Path.GetDirectoryName(path));
			}
		}

		private void listboxHistory_DeleteItem_Click(object sender, RoutedEventArgs e)
		{
			MenuItem menuItem = sender as MenuItem;
			if (menuItem != null || menuItem.DataContext != null)
			{
				string name = menuItem.DataContext.ToString();
				ConfigSettings.GetInstance().History.RemoveExperimentHistory(name);
			}
		}

		private void listboxHistory_ClearAll_Click(object sender, RoutedEventArgs e)
		{
			ConfigSettings.GetInstance().History.ExperimentHistory.Clear();
		}

		private void listboxHistoryTemplateItem_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
		{
			TextBlock textBlock = sender as TextBlock;
			if (textBlock != null && textBlock.DataContext != null)
			{
				MainWindow.OpenExperimentFromHomeControl(textBlock.DataContext.ToString());
			}
		}

		private void listboxHistoryTemplate_ClearAll_Click(object sender, RoutedEventArgs e)
		{
			ConfigSettings.GetInstance().History.ExperimentTemplateHistory.Clear();
		}
	}
}
